package com.china08.yxyapi.api.manage.sch.platform.website.article;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ArticeBanchUpdateShowDateController extends YxyApiBaseController {

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/article/banch/update/showDate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        List<String> articeIds = reqModel.articeIds;
        Date showDate = reqModel.showDate;
        System.out.println(articeIds);

        for (String id : articeIds) {
            SwsArticle swsArticle = swsArticleRepository.findById(id);
            if (null != swsArticle) {
                swsArticle.setShowDate(showDate);
                swsArticleRepository.save(swsArticle);
            }
        }
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public Date showDate;
        public List<String> articeIds;

        @Override
        public void autoCheck() {
            Assert.isTrue(null != showDate, "显示时间不能为空");
            Assert.isTrue(articeIds.size() > 0, "请选择要修改的文章");
        }
    }
}
